package com.rrs.client

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.common.collect.HashBiMap
import com.rrs.client.databinding.ActivityMainBinding
import com.rrs.client.fragment.IndexFragment
import com.rrs.client.fragment.ShopFragment
import com.rrs.client.fragment.UserFragment
import com.xuexiang.xui.XUI
import kotlin.collections.ArrayList

class MainActivity : AppCompatActivity() {

    class Pager(
        val navId: Int,
        val fragment: Fragment
    )

    private lateinit var binding: ActivityMainBinding
    private lateinit var viewPager: ViewPager2
    private lateinit var pagerList: ArrayList<Pager>
    private lateinit var pagerMap: HashBiMap<Int, Int>    // <index, pagerId>
    lateinit var fragmentList: ArrayList<Fragment>
    lateinit var systemBars: Insets

    @SuppressLint("InternalInsetResource")
    override fun onCreate(savedInstanceState: Bundle?) {
        //xiu init
        XUI.initTheme(this)
        super.onCreate(savedInstanceState)

        //viewbinding init
        binding = ActivityMainBinding.inflate(layoutInflater)
        enableEdgeToEdge()
        setContentView(binding.root)

        //databinding init
        //val activityMainDataBinding =
        //    DataBindingUtil.setContentView<ViewDataBinding>(this, R.layout.activity_main)
        //activityMainDataBinding.setVariable(BR.text, "111222")

        val insets =
            ViewCompat.setOnApplyWindowInsetsListener(findViewById(binding.main.id)) { v, insets ->
                systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                insets
            }


        pagerList = ArrayList()
        pagerList.add(Pager(R.id.index, IndexFragment(null)))
        pagerList.add(Pager(R.id.shop, ShopFragment(null)))
        pagerList.add(Pager(R.id.user, UserFragment(null)))


        pagerMap = HashBiMap.create<Int, Int>()
        for (i in pagerList.indices) {
            pagerMap.put(i, pagerList[i].navId)
        }

        initViewPage()
        initNav()


    }

    /**
     * 初始化viewpage
     */
    private fun initViewPage() {

        fragmentList = ArrayList()
        for (pager in pagerList) {
            fragmentList.add(pager.fragment)
        }

        viewPager = binding.pager
        viewPager.adapter = FragAdapter(this, fragmentList)
        //viewPager.isUserInputEnabled = false  //禁止用户左右滑动切换界面
        // 用户手动切换页面时，底部导航栏也会切换
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.nav.selectedItemId = pagerMap[position]!!
            }
        }
        )
    }

    /**
     * Fragment 适配器
     */
    open class FragAdapter(
        fa: FragmentActivity,
        private val list: MutableList<Fragment>
    ) : FragmentStateAdapter(fa) {

        override fun getItemCount(): Int {
            return list.size
        }

        override fun createFragment(position: Int): Fragment {
            return list[position]
        }

    }

    /**
     * 初始化nav
     */
    private fun initNav() {
        // 点击底部导航栏按钮，跳转到对应页面
        binding.nav.setOnItemSelectedListener { item ->
            binding.pager.currentItem = pagerMap.inverse()[item.itemId]!!
            return@setOnItemSelectedListener true
        }
    }

}